package com.sky.controller.admin;

import com.sky.properties.AliyunOSSProperties;
import com.sky.result.Result;
import com.sky.utils.AliyunOSSUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

@RestController
@Slf4j
public class UploadController {
    @Autowired
    private AliyunOSSProperties aliyunOSSProperties;

    @PostMapping("/admin/common/upload")
    public Result upload(@RequestBody MultipartFile file) throws Exception {
        log.info("文件上传开始,文件名:{}",file.getOriginalFilename());
        //获取字节数组
        byte[] bytes = file.getBytes();
        //获取文件名及后缀
        String originalFilename = file.getOriginalFilename();
        String extName = originalFilename.substring(originalFilename.lastIndexOf("."));
        //完成文件上传并返回文件访问地址
        String url = AliyunOSSUtils.upload(aliyunOSSProperties.getEndpoint(), aliyunOSSProperties.getBucketName(), bytes, extName);
        return Result.success(url);
    }
}
